/**
 * 
 */
package truerefactor.refactor;

import java.util.ArrayList;
import java.util.List;

import truerefactor.graph.ClassNode;
import truerefactor.graph.CodeGraph;
import truerefactor.graph.CodeNode;
import truerefactor.ui.DisplayInfo;

/**
 * Refactoring - The abstract base class from which all refactoring technique
 * implementation are derived. It forms the parent class of the hierarchy in the
 * strategy pattern used for refactorings.
 * 
 * @author Isaac Griffith
 */
public abstract class Refactoring implements DisplayInfo {

    /**
     * the complete name of this refactoring technique
     */
    protected String name;
    /**
     * an acronym name for this refactoring
     */
    protected String shortName;
    /**
     * brief description of the refactoring technique
     */
    protected String description;
    /**
     * list of operands this refactoring uses
     */
    protected List<CodeNode> operands;

    /**
     * Creates a new Refactoring instance
     */
    public Refactoring()
    {
        operands = new ArrayList<CodeNode>();
    }

    /**
     * Executes this refactoring on the provided graph using the provided
     * operand code nodes.
     * 
     * @param graph
     *            The CodeGraph which is to be modified by this refactoring
     *            command.
     */
    public abstract void execute(CodeGraph graph);

    /*
     * (non-Javadoc)
     * @see truerefactor.ui.DisplayInfo#getName()
     */
    @Override
    public String getName()
    {
        return name;
    }

    /*
     * (non-Javadoc)
     * @see truerefactor.ui.DisplayInfo#getDescription()
     */
    @Override
    public String getDescription()
    {
        return description;
    }

    /**
     * Sets the set of operands the be the provide list of CodeNodes
     * 
     * @param nodes
     *            new set of operands
     */
    public void setOperands(List<CodeNode> nodes)
    {
        if (operands == null)
        {
            operands = new ArrayList<CodeNode>();
        }
        operands.clear();

        if (nodes != null)
            operands.addAll(nodes);
    }

    /*
     * (non-Javadoc)
     * @see truerefactor.ui.DisplayInfo#getShortName()
     */
    @Override
    public String getShortName()
    {
        return shortName;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append(name);
        for (int i = 0; i < operands.size(); i++)
        {
            CodeNode operand = operands.get(i);
            sb.append(" " + operand + " ");
        }

        return sb.toString();
    }
}
